<template>
  <div class="parent">
    <!-- 导航栏 -->
    <div class="navLeft">
      <el-tabs v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visite"></el-tab-pane>
      </el-tabs>
    </div>
    <!-- 日历部分 -->
    <div class="dateRight">
      <span @click="setDay">今日</span>
      <span @click="setWeek">本周</span>
      <span @click="setMonth">本月</span>
      <span @click="setYear">本年</span>
      <!-- 日历 -->
      <el-date-picker
        v-model="date"
        style="width: 250px"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        size="mini"
        value-format="yyyy-MM-dd"
      >
      </el-date-picker>
    </div>
    <!-- 柱状图与排名 -->
    <div class="barRank">
      <el-row :gutter="10">
        <el-col :span="18">
          <div class="salebar" ref="salecharts"></div>
        </el-col>
        <el-col :span="6" style="margin-top: 20px" class="right">
          <h3>门店{{ title }}排名</h3>
          <ul>
            <li>
              <span class="rindex">1</span>
              <span>肯德基</span>
              <span class="rvalue">923,456</span>
            </li>
            <li>
              <span class="rindex">2</span>
              <span>麦当劳</span>
              <span class="rvalue">865,454</span>
            </li>
            <li>
              <span class="rindex">3</span>
              <span>华莱士</span>
              <span class="rvalue">722,126</span>
            </li>
            <li>
              <span style="padding: 0 5px">4</span>
              <span>海底捞</span>
              <span class="rvalue">627,678</span>
            </li>
            <li>
              <span style="padding: 0 5px">5</span>
              <span>西贝莜面村</span>
              <span class="rvalue">556,786</span>
            </li>
            <li>
              <span style="padding: 0 5px">6</span>
              <span>汉堡王</span>
              <span class="rvalue">423,656</span>
            </li>
            <li>
              <span style="padding: 0 5px">7</span>
              <span>真功夫</span>
              <span class="rvalue">323,984</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
import * as echarts from "echarts";

export default {
  data() {
    return {
      activeName: "",
      myecharts1: null,
      date: [],
    };
  },
  mounted() {
    this.activeName = "sale";
    this.myecharts1 = echarts.init(this.$refs.salecharts);
    this.myecharts1.setOption({
      title: {
        text: "销售额趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          color: "skyblue",
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [],
        },
      ],
    });
  },
  methods: {
    //获取今日
    setDay() {
      let day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    // 获取本周
    setWeek() {
      let start = dayjs().day(1).format("YYYY-MM-DD");
      let end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    //获取本月
    setMonth() {
      let start = dayjs().startOf("month").format("YYYY-MM-DD");
      let end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    //获取本年
    setYear() {
      let start = dayjs().startOf("year").format("YYYY-MM-DD");
      let end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
  computed: {
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
  },
  watch: {
    title() {
      this.myecharts1.setOption({
        title: { text: this.title + "趋势" },
        xAxis: {
          data:
            this.title == "销售额"
              ? [
                  "一月",
                  "二月",
                  "三月",
                  "四月",
                  "五月",
                  "六月",
                  "七月",
                  "八月",
                  "九月",
                  "十月",
                  "十一月",
                  "十二月",
                ]
              : [
                  "1月",
                  "2月",
                  "3月",
                  "4月",
                  "5月",
                  "6月",
                  "7月",
                  "8月",
                  "9月",
                  "10月",
                  "1月",
                  "12月",
                ],
        },
        series: [
          {
            data:
              this.title == "销售额"
                ? [59, 52, 200, 334, 390, 330, 220, 110, 98, 100, 234, 120]
                : [159, 32, 100, 134, 290, 330, 210, 120, 68, 90, 134, 128],
          },
        ],
      });
    },
  },
};
</script>

<style scoped>
.parent {
  position: relative;
}
.dateRight {
  position: absolute;
  top: 4px;
  right: 0;
}
.dateRight span {
  margin-right: 10px;
  cursor: pointer;
}
.salebar {
  width: 100%;
  height: 300px;
  margin-top: 20px;
}
.right span {
  margin: 0 10px;
}
ul {
  list-style: none;
  width: 100%;
  padding: 0px;
}
ul li {
  height: 8%;
  margin: 20px 0px;
}
.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
}
.rvalue {
  float: right;
}
</style>